<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>递归解决深拷贝</title>
</head>
<body>
<script>
	function deepClone(obj) {
		let objClone = Array.isArray(obj) ? [] : {};
		if(obj && typeof obj === 'object') {
			for (let key in obj) {
				if(obj.hasOwnProperty(key)){
					//判断obj子集是否为对象， 如果是 递归复制
					if(obj[key] && typeof[key] === 'object') {
						objClone[key] = deepClone(obj[key])
					} else {
						//如果不是，简单复制
						objClone[key] = obj[key];
					}
				}
			}
		}
		return objClone;
	}

	let a = [1, 2, 3, 4];
	let b = deepClone(a);
	a[0] = 2;
	console.log(a)
	console.log(b)
</script>

</body>
</html>